package com.star.star.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.star.common.enums.AuditStatus;
import com.star.common.utils.Dates;
import com.star.star.dto.StarArtEmpowerSaveOrUpdateDTO;
import com.star.star.entity.StarArt;
import com.star.star.entity.StarArtEmpower;
import com.star.star.mapper.StarArtEmpowerMapper;
import com.star.star.service.StarArtEmpowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.star.service.StarArtService;
import com.star.star.struct.StarArtStruct;
import com.star.star.vo.StarArtEmpowerDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 艺人作品授权 服务实现类
 *
 * @author yangzj
 */
@Service
public class StarArtEmpowerServiceImpl extends ServiceImpl<StarArtEmpowerMapper, StarArtEmpower> implements StarArtEmpowerService {

    @Autowired
    private StarArtService starArtService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(StarArtEmpowerSaveOrUpdateDTO dto) {
        StarArt starArt = Assert.notNull(starArtService.getById(dto.getArtId()), "作品不存在");
        Assert.isTrue(AuditStatus.OK.getCode().equals(starArt.getAuditStatus()),"作品审核通过后才能授权");
        boolean exists = this.lambdaQuery().eq(StarArtEmpower::getArtId, dto.getArtId()).exists();
        if (exists) {
            this.update(Wrappers.<StarArtEmpower>lambdaUpdate().eq(StarArtEmpower::getArtId, dto.getArtId())
                    .set(StarArtEmpower::getEmpowerUrls, dto.getEmpowerUrls())
                    .set(StarArtEmpower::getBeginEffective, dto.getBeginEffective())
                    .set(StarArtEmpower::getEndEffective, dto.getEndEffective())
                    .set(StarArtEmpower::getRemark, dto.getRemark()));
        } else {
            this.save(StarArtStruct.INSTANCE.to(dto));
        }
        // 更新作品是否授权
        boolean isIn = DateUtil.isIn(Dates.toDate(LocalDate.now()), Dates.toDate(dto.getBeginEffective()), Dates.toDate(dto.getEndEffective()));
        starArtService.updateBatchEmpower(Collections.singletonList(new StarArt().setId(dto.getArtId()).setIsEmpower(isIn)), false);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void scheduleEmpower() {
        // 找出未授权且在授权时间范围的作品 和 找出已授权但不在授权时间范围的作品
        List<StarArt> starArts = baseMapper.listWaitingEmpower();
        // 取反
        starArts.forEach(c-> c.setIsEmpower(!c.getIsEmpower()));
        starArtService.updateBatchEmpower(starArts, true);
    }

    @Override
    public StarArtEmpowerDetailVO getDetail(Long artId) {
        return baseMapper.getDetail(artId);
    }

    @Override
    public List<StarArtEmpower> listByArtIds(List<Long> artIds) {
        return this.list(Wrappers.<StarArtEmpower>lambdaQuery().in(StarArtEmpower::getArtId, artIds));
    }
}
